
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
// const TerserPlugin = require("terser-webpack-plugin");

const path = require('path')
function resolve (dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  outputDir: process.env.NODE_ENV === 'production' ? 'dist' : 'dist',
  lintOnSave: false,
  runtimeCompiler: true,
  transpileDependencies: ['vue-virtual-scroller'],
  productionSourceMap: false,


  devServer: {
    port: '8002',
    proxy: {
      '/admin-api': {
        // target: 'http://192.168.3.11:58881', // 小勇
        // target: 'http://192.168.3.23:58881', // 晓威
        // target: 'http://192.168.3.21:58881', // 来勇
        // target: 'http://192.168.3.20:58881', // 立花
        // target: 'http://192.168.3.33:58881', // 靖凯
        target: 'http://121.37.80.21:8881/integration', // 测试环境
        changeOrigin: true,
      },
      '/rfcs': {
        target: 'http://10.233.146.159:17201',
        changeOrigin: true
      }
    }
  },

  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "@/assets/sass/mixin.scss";'
      }
    }
  },

  chainWebpack: (config) => {
    const isProd = process.env.NODE_ENV === 'production'
    if (isProd) {
      // 配置如何展示性能提示
      config.performance
        .hints('warning')
        .maxEntrypointSize(3000000)
        .maxAssetSize(1000000)
        .assetFilter((assetFilename) => {
          // 配置计算性能提示的文件类型
          return assetFilename.endsWith('.css') || assetFilename.endsWith('.js')
        })

      config.optimization.splitChunks({
        cacheGroups: {
          common: {
            name: 'chunk-common', // 打包后的文件名
            chunks: 'initial',
            minChunks: 2,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 1,
            reuseExistingChunk: true
          },
          vendors: {
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            chunks: 'initial',
            priority: 2,
            reuseExistingChunk: true,
            enforce: true
          },
          'element-ui': {
            name: 'element-ui',
            test: /[\\/]node_modules[\\/]element-ui[\\/]/,
            chunks: 'initial',
            priority: 3,
            reuseExistingChunk: true,
            enforce: true
          },
          echarts: {
            name: 'echarts',
            test: /[\\/]node_modules[\\/]echarts[\\/]/,
            chunks: 'initial',
            priority: 3,
            reuseExistingChunk: true,
            enforce: true
          }
        }
      })

      config.plugin('clean-webpack-plugin').use(CleanWebpackPlugin)

    }

    config.module
      .rule('xlsx-file')
      .test(/.xlsx$/)
      .use('xlsx-loader')
      .loader('file-loader')
    config.module
      .rule('zip-file')
      .test(/.zip$/)
      .use('zip-loader')
      .loader('file-loader')

    // 调试模式启动包体积分析
    if (process.env.NODE_ENV === 'test') {
      config
        .plugin('webpack-bundle-analyzer')
        .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    }

    // 添加别名
    config.resolve.alias.set('@axios', '@/api/axios')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  }
}
